package chelydra.sharelydra.server.web;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import chelydra.sharelydra.server.ejb.SLSManager;
import javax.servlet.RequestDispatcher;
import chelydra.sharelydra.server.ejb.SLSNotification;
import chelydra.sharelydra.server.ejb.SLSUser;
import javax.ejb.EJBException;
import javax.persistence.NoResultException;
/**
 *
 * @author scallion
 */
public class SLSServlet extends HttpServlet {

	protected SLSManager manager;
	protected String action_name;
	protected RequestDispatcher rd;
	protected String fail_case;

   
  protected void initManager(HttpServletRequest request) throws ServletException, IOException {
		SLSManager m;
		try {
			m = SLSLocator.getLocator().getManager();
		} catch (Exception e) {
			e.printStackTrace();
			throw new ServletException("ShareLydra is actually out of control! RUN AWAY NOW!");
		}
		this.manager = m;
	}

	protected SLSUser initAttributes(HttpServletRequest request,HttpServletResponse response)
  throws ServletException, IOException {
		SLSUser user = null;

		if ((request.getParameter("login") ==null) || (request.getParameter("password") ==null)) {
			request.setAttribute("fail_case","Login and password are required");
			this.rd = request.getRequestDispatcher("/WEB-INF/jsp/fail.jsp");
			this.rd.forward(request, response);
			return null;
		}

		try {
			user = this.manager.retrieveUserWithLoginAndPassword(request.getParameter("login"), request.getParameter("password"));
			if(user==null){
				request.setAttribute("fail_case","Incorrect login or password");
				this.rd = request.getRequestDispatcher("/WEB-INF/jsp/fail.jsp");
				return null;
			}
			
			request.setAttribute("action_name", this.action_name);
			request.setAttribute("user", user);
			request.setAttribute("notifications", this.manager.retrieveNotificationsForUser(user));
			request.setAttribute("friends", user.getFriends());

			this.rd = request.getRequestDispatcher("/WEB-INF/jsp/successful.jsp");
			this.manager.deleteNotificationsForUser(user);
			this.log(request, user);
			return user;

		} catch (NoResultException nre) {
			request.setAttribute("fail_case","Incorrect login or password");
			this.rd = request.getRequestDispatcher("/WEB-INF/jsp/fail.jsp");
			nre.printStackTrace(System.err);
			return null;
			
		} catch (EJBException ee) {
			request.setAttribute("fail_case","Incorrect login or password");
			this.rd = request.getRequestDispatcher("/WEB-INF/jsp/fail.jsp");
			ee.printStackTrace(System.err);
			return null;
		}
		
  }
	
	protected void processRequest(HttpServletRequest request, HttpServletResponse response)
  throws ServletException, IOException {
		java.io.PrintWriter out = response.getWriter();
		out.print("merde");
  }

	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
  throws ServletException, IOException {
    this.processRequest(request, response);
  }

	@Override
  protected void doPost(HttpServletRequest request, HttpServletResponse response)
  throws ServletException, IOException {
    this.processRequest(request, response);
  }

	private void log(HttpServletRequest request, SLSUser user) {
		System.out.println("ACTION PERFORMED :: "+this.action_name);
		System.out.println("\tip :: "+request.getRemoteAddr());
		System.out.println("\tuser :: "+user.getLogin()+"\n");
	}
}
